<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改</title>
<link rel="stylesheet" href="${ctx }/resources/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="${ctx }/resources/css/public.css" media="all" />
<link rel="stylesheet" href="${ctx }/resources/zTree/css/metroStyle/metroStyle.css" type="text/css">
<link rel="stylesheet" type="text/css" href="${ctx }/resources/plugin/css/index.css">
</head>
<body class="childrenBody">
	<form class="layui-form" action="#" id="frm" method="post" lay-filter="frm">
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">选择部门</label>
				<div class="layui-input-inline">
					<div id="deptid" name="deptid" class="layui-form-select select-tree"></div>
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">排序码</label>
				<div class="layui-input-inline">
					<input type="text" name="ordernum" lay-verify="number"
					  value="${user.ordernum }"	autocomplete="off" class="layui-input">
				<input type="hidden" name="id" value="${user.id }">
				</div>
			</div>
		</div>

	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">领导部门</label>
			<div class="layui-input-inline">
				<div id="leaderdeptid" name="leaderdeptid" class="layui-form-select select-tree"></div>
			</div>
		</div>	
		<div class="layui-inline">
			<label class="layui-form-label">选择领导</label>
			<div class="layui-input-inline">
				<select name="mgr" id="mgr" lay-filter="mgr">
					<option value=""></option>
				</select>
			</div>
		</div>	
	</div>
	
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">用户姓名</label>
			<div class="layui-input-inline">
				<input type="text" name="name" id="name" lay-verify="required"
				  value="${user.name }"	autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">登陆名称</label>
			<div class="layui-input-inline">
				<input type="text" name="loginname" id="loginname"
				   value="${user.loginname }"	autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
	
	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">用户地址</label>
		<div class="layui-input-block">
			<input type="text" name="address" 
			 value="${user.address }"	autocomplete="off" class="layui-input">
		</div>
	</div>
	
	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">用户备注</label>
		<div class="layui-input-block">
			<textarea placeholder="请输入内容" name="remark" class="layui-textarea">${user.remark }</textarea>
		</div>
	</div>
	
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">性别</label>
			<div class="layui-input-inline">
				<input type="radio" name="sex" value="1" title="男" ${user.sex==1?'checked':''} >
				<input type="radio" name="sex" value="0" title="女" ${user.sex==0?'checked':''}> 
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">是否可用</label>
			<div class="layui-input-inline">
				<input type="radio" name="available" value="1" title="是" ${user.available==1?'checked':'' }>
				<input type="radio" name="available" value="0" title="否" ${user.available==0?'checked':'' }> 
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">入职时间</label>
			<div class="layui-input-inline">
				<input type="text" name="hiredate" id="hiredate"  
				  value='<fmt:formatDate value="${user.hiredate }" pattern="yyyy-MM-dd"/>'  autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>


		<div class="layui-form-item">
			<div class="layui-input-block" style="text-align: center;">
				<!--lay-submit标记当按钮是一个提交按钮   -->
				<a href="javascript:void(0)" class="layui-btn" lay-submit=""  lay-filter="userSubmit">提交</a>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</body>
<script type="text/javascript" src="${ctx }/resources/layui/layui.js"></script>
<script type="text/javascript" src="${ctx }/resources/zTree/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${ctx }/resources/zTree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${ctx }/resources/plugin/js/selectTree.js"></script>
<script type="text/javascript">
var form;
layui.use([ 'form','jquery','layer','laydate' ], function() {
	form=layui.form;//得到form对象
	var $=layui.jquery;
	var laydate=layui.laydate;
	var layer = parent.layer === undefined ? layui.layer : parent.layer;
	
	//时间选择器的绑定
	laydate.render({
		elem:'#hiredate'
	});
	
	//添加提交事件
	form.on("submit(userSubmit)",function(data){
		var params=$("#frm").serialize();
		$.post("${ctx}/user/updateUser.action?"+params,function(obj){
			//console.log(params);
			layer.msg(obj.msg);
			parent.tableIns.reload();//刷新表格
			//关闭窗口
			var index=layer.getFrameIndex(window.name);
			layer.close(index);
		}) 
	});
	 //监听用户姓名的失去焦点事件
	$("#name").on("blur",function(){
		var name=this.value;
		//请求后台，中文转换成拼音
		$.post("${ctx}/user/initUserLoginNameUseUserName.action",{name:name},function(obj){
			$("#loginname").val(obj);
		}) 
	});
	
});	
	$(document).ready(function () {
       initDeptTree();
    });
	
	function initDeptTree() {
		//加载可用部门
		$.post("${ctx}/dept/loadDeptTree.action?available=1",function(zNodes){
			initSelectTree("deptid",zNodes, false);
			initSelectTree("leaderdeptid",zNodes, false);
			//选中用户部门
			var userDeptId="${user.deptid}";
			var treeObj = $.fn.zTree.getZTreeObj("deptidTree");
			var node = treeObj.getNodeByParam("id", userDeptId, null);//根据id得到应该被选中的节点
			treeObj.selectNode(node); //选中节点
			//重新渲染
			onClick(event,"deptidTree",node);
			
			var leaderId="${user.mgr}";  //领导id
			//根据领导id查领导部门id
			$.post("${ctx}/user/queryUserById.action",{id:leaderId},function(obj){
				var leaderDeptId=obj.deptid;
				var leadertreeObj = $.fn.zTree.getZTreeObj("leaderdeptidTree");
				var leadernode = leadertreeObj.getNodeByParam("id", leaderDeptId, null);//根据id得到应该被选中的节点
				leadertreeObj.selectNode(leadernode); //选中节点 
				//重新渲染
				onClick(event,"leaderdeptidTree",leadernode);
			},"json");
			
			$(".layui-nav-item a").bind("click", function () {
	            if (!$(this).parent().hasClass("layui-nav-itemed") && !$(this).parent().parent().hasClass("layui-nav-child")) {
	                $(".layui-nav-tree").find(".layui-nav-itemed").removeClass("layui-nav-itemed")
	            }
	        })
		})
	}
	//根据部门id查部门的所有用户
	function initMgr(deptid) {
		$.post("${ctx}/user/loadUserByDeptId.action",{deptid:deptid},function(obj){
			var html="";
			for (var i = 0; i < obj.length; i++) {
				var leaderId=${user.mgr}
				if(leaderId==obj[i].id){
					html+=" <option selected value="+obj[i].id+">"+obj[i].name+"</option>"
				}else{
				html+=" <option value="+obj[i].id+">"+obj[i].name+"</option>"
				}
			}
			$("#mgr").html(html);
			form.render("select"); //渲染
		},"json")
	}


</script>
</html>